<?php

namespace app\api\controller;

use app\api\library\CommentException;
use think\Config;
use think\Exception;
use app\admin\model\cms\Diyform;
use app\admin\model\cms\Chart as ChartModel ;
use think\Validate;
use think\Db;

/**
 * 图表
 */
class Chart extends Base
{
        protected $noNeedLogin = ['index', 'getList','getIndex'];

    public function _initialize()
    {
        parent::_initialize();
    }
    
    public function getTopList()
    {
        $result = [];
        $TopTables = ChartModel::where('weigh','>',0)->order('weigh asc')->limit(3)->select();
        foreach($TopTables as $key => $table){
            $result[$key]['data'][] = $this->getChartList($table['id'], $table['name']);
            $result[$key]['table_name'] = $table['name'];
            $result[$key]['desc'] = $table['desc'];
            
        }
    //   print_r($result);
        $this->success('',$result);
    }
    
    public function getList()
    {
        $result = [];
        $Tables = ChartModel::whereNotIn('weigh',[1,2,3])->order('weigh asc')->select();
        if(!empty($Tables) && count($Tables) >0){
            foreach($Tables as $key =>$table){ 
                $result[$key]['data'][] = $this -> getChartList($table['id'],$table['name']);
                $result[$key]['table_name'] = $table['name'];
                $result[$key]['desc'] = $table['desc'];
            }
        }
        $this->success('',$result);
    }

    public function getChartList($cid,$name)
    {
        $param = $this->request->param();
        $cid =  !empty($cid)? $cid: 1;
        $tables = Diyform::where('parent_id',$cid)->field('table,name')->select();
        if (isset($param['startTime']) && $param['endTime']) {
            $start = $param['startTime'];
            $end = $param['endTime'];
            
        }else{
            //默认最近一年
            $end = date('Y-m-d');
            $start = date('Y-m-d', strtotime('-12 months'));
        } 
        if(!empty($tables) && count($tables) >0){
            $chartList = [];
            foreach($tables as $key =>$table){
               $chartInfo = Db::name($table['table'])->whereTime('datetime', 'between', [$start, $end])->select();
               $chart_name = $table['name'];
               if(!empty($chartInfo)){
                   
                   $chartList[$key] =$this->format_data($chartInfo,$chart_name);
               }else{
                   $chartList[$key]['name'] = [];
                   $chartList[$key]['val'] = [];
                   $chartList[$key]['line'] = $chart_name;
               }
            }
            return $chartList;
        }else{
          
            return [];
        }
    }
    public function format_data($chartInfo,$table)
    {
        $lineArr =[];
        foreach($chartInfo as $data){
                $lineArr['name'][] = $data['datetime'];
                $lineArr['val'][] = $data['field1'];
                $lineArr['line'] = $table;
        }
        return $lineArr;
    }
    public function format_data1($chartInfo,$table)
    {
        if(!empty($chartInfo)){
            $sql = "SHOW FULL COLUMNS FROM fa_{$table}";
            $result = Db::query($sql);
            $fileds = [];
            if(!empty($result)){
                $d = ['id','datetime','createtime','updatetime','memo'];
                $comment = [];
                $i = 0;
                foreach($result as $val)
                {
                    if(!in_array($val['Field'],$d)){
                        $fileds[] = $val['Field'];
                        $comment[] = $val['Comment'];
                    }
                }
//                $fileds = array_diff($fileds,$d);s
                if(count($fileds)>1){
                    $lineArr =[];
                    foreach($chartInfo as $data){
                        foreach ($fileds as $k=>$filed){
                            $lineArr[$filed]['name'][] = $data['datetime'];
                            $lineArr[$filed]['val'][] = $data[$filed];
                            $lineArr[$filed]['table'] = $comment[$k];
                        }
                    }
                    $this->success('',$lineArr);
                }
            }
            $this->success('',[]);        
        }
    }
    public function getIndex()
    {
        $list = Diyform::where('id', '>', 0)->field('id,parent_id,name,title,createtime')->select();
        $this->success('',$list);
    }
    public function getIdxList()
    {
        $cid = 1;
        $param = $this->request->param();
        $cid =  isset($param['cid'])? $param['cid'] : 2;
        $tables = Diyform::where('parent_id',$cid)->value('table');
        if (isset($param['startTime']) && $param['endTime']) {
            $start = $param['startTime'];
            $end = $param['endTime'];
            Db::name($tables)->whereTime('datetime', 'between', [$start, $end])->select();
        }else{
            
            $chartInfo = Db::name($tables)->select();//  var_dump(Db::getLastSql());exit;
        }
        if(!empty($chartInfo)){
            $sql = "SHOW FULL COLUMNS FROM fa_{$tables}";
            $result = Db::query($sql);
            $fileds = [];
            if(!empty($result)){
                $d = ['id','datetime','createtime','updatetime','memo'];
                $comment = [];
                $i = 0;
                foreach($result as $val)
                {
                    if(!in_array($val['Field'],$d)){
                        $fileds[] = $val['Field'];
                        $comment[] = $val['Comment'];
                    }
                }
//                $fileds = array_diff($fileds,$d);s
                if(count($fileds)>1){
                    $lineArr =[];
                    foreach($chartInfo as $data){
                        foreach ($fileds as $k=>$filed){
                            $lineArr[$filed]['name'][] = $data['datetime'];
                            $lineArr[$filed]['val'][] = $data[$filed];
                            $lineArr[$filed]['table'] = $comment[$k];
                        }
                    }
                    $this->success('',$lineArr);
                }
            }
            $this->success('',[]);
        }
        $this->success('',[]);
    }
    public function getList_old()
    {
        //$cid = 2;
        $param = $this->request->param();
        $cid =  isset($param['cid'])? $param['cid'] : 2;
        $tables = Diyform::where('parent_id',$cid)->value('table');
        if (isset($param['startTime']) && $param['endTime']) {
            $start = $param['startTime'];
            $end = $param['endTime'];
            Db::name($tables)->whereTime('datetime', 'between', [$start, $end])->select();
        }else{
            
            $chartInfo = Db::name($tables)->select();//  var_dump(Db::getLastSql());exit;
        }
        if(!empty($chartInfo)){
            $sql = "SHOW FULL COLUMNS FROM fa_{$tables}";
            $result = Db::query($sql);
            $fileds = [];
            if(!empty($result)){
                $d = ['id','datetime','createtime','updatetime','memo'];
                $comment = [];
                $i = 0;
                foreach($result as $val)
                {
                    if(!in_array($val['Field'],$d)){
                        $fileds[] = $val['Field'];
                        $comment[] = $val['Comment'];
                    }
                }
//                $fileds = array_diff($fileds,$d);s
                if(count($fileds)>1){
                    $lineArr =[];
                    foreach($chartInfo as $data){
                        foreach ($fileds as $k=>$filed){
                            $lineArr[$filed]['name'][] = $data['datetime'];
                            $lineArr[$filed]['val'][] = $data[$filed];
                            $lineArr[$filed]['table'] = $comment[$k];
                        }
                    }
                    $this->success('',$lineArr);
                }
            }
            $this->success('',[]);
        }
        $this->success('',[]);
    }
}